/************************ 
Stata do file for 
	"The fiscal politics of turnover and tenure: partisan competition and interterm cycles"
The observations in the dataset are terms. 
Terms are observed in their end year. 
************************/


* use "JPIPE_Johnson_2024_dataset.dta", clear 

  
set scheme s1mono, permanently



/********************
* TSSET  by term - country year
********************/
rename ctry_code  c_abbrev_name
egen c_id = group(countryname) 
egen term_ts_id = seq()         
tsset c_id term_ts_id, generic



/************************ 
POLITICAL VARIABLES
************************/
gen euro2 = eurozone
replace euro2 = 1 if f.eurozone==1 & year>1997
replace euro2 = 2 if l.euro2==1 & l2.euro2==0 
replace euro2 = 2 if l.euro2==2 
replace euro2 = 1 if countryname=="Sweden" & year>1997 
replace euro2 = 1 if countryname=="Denmark" & year>1997 
replace euro2 = 1 if countryname=="Norway" & year>1997 
replace euro2 = 1 if countryname=="United Kingdom" & year>1997 

list country year euro2 if euro2>0


replace nfrules = 0 if nfrules==. & year<1990


* neg_ft and neg_lt are used in the violinplots (kernel densities)
gen neg_ft = -ft 
gen neg_lt = -lt 

gen ftlt0 = 1 
replace ftlt0 = 0 if ft==1 | lt==1 




/*********************************************************************************
TABLE 3 - Economic models - nterms>5

Steps for each regression: 
1) First there is a preliminary estimate for each regession. 
2) temp_include is used to identify & exclude countries that have <5 terms in that sample
(some countries with 5 terms had extreme outliers - e.g., Slovenia, Jamaica)
3) Regression is re-estimated using smaller sample
4) The residuals are coded in "res_*", and "samp_*" is a dummy for terms in that sample
5) Then, there's a serial correlation test, adding residuals into the regression.	 

* note: xtpcse doesn't work because terms end in different years
*********************************************************************************/


***** Regression 1: D1 
reg d.d1 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d1 l.g1 l.u l.cpi l.bcrisis l.euro2 , robust
	gen temp_include1 = 0
	replace temp_include1=1 if e(sample)
	egen temp_include2 = sum(temp_include1), by(country)
	replace temp_include2  = 0 if temp_include2<5
reg d.d1 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d1 l.g1 l.u l.cpi l.bcrisis l.euro2  if temp_include2>0, robust
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) ctitle(d1) alpha(.05) symbol(*)
predict res_r1, r
gen samp_r1 = 0
replace samp_r1 = 1 if e(sample)
drop temp*


***** Regression 2: D2 
reg d.d2 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d2 l.g1 l.u l.cpi l.bcrisis l.euro2  , robust
	gen temp_include1 = 0
	replace temp_include1=1 if e(sample)
	egen temp_include2 = sum(temp_include1), by(country)
	replace temp_include2  = 0 if temp_include2<5
reg d.d2 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d2 l.g1 l.u l.cpi l.bcrisis l.euro2  if temp_include2>0, robust
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d2)  alpha(.05) symbol(*)
predict res_r2, r
gen samp_r2 = 0
replace samp_r2 = 1 if e(sample)
drop temp*


***** Regression 3: D3
reg d.d3 d.g1 d.u d.cpi l.d3 l.g1 l.u l.cpi d.bcrisis l.bcrisis d.euro2 l.euro2 , robust
	gen temp_include1 = 0
	replace temp_include1=1 if e(sample)
	egen temp_include2 = sum(temp_include1), by(country)
	replace temp_include2  = 0 if temp_include2<5
reg d.d3 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d3 l.g1 l.u l.cpi l.bcrisis l.euro2  if temp_include2>0, robust
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*) 
predict res_r3, r
gen samp_r3 = 0
replace samp_r3 = 1 if e(sample)
drop temp*


* Regression 3 (D3) without presidential systems (not shown)
reg d.d3 d.g1 d.u d.cpi d.bcrisis d.euro2 l.d3 l.g1 l.u l.cpi l.bcrisis l.euro2  if parl==1, robust
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3-samp_r1)  alpha(.05) symbol(*) 




	
	
/************************ 
Table 2:  LIST COUNTRIES & YEARS OF COVERAGE BY SAMPLE 
************************/

	gen r1_year_start = 0 
	replace r1_year_start = year+1 if samp_r1==0 & f.samp_r1==0 & f2.samp_r1==1
	egen temp1 = max(year), by(samp_r1 c_id)
	replace temp1 = 0 if samp_r1!=1
	egen r1_year_end = max(temp1), by(country)
list country year r1_year_start r1_year_end if r1_year_start !=0

	drop temp1
	gen r2_year_start = 0 
	replace r2_year_start = year+1 if samp_r2==0 & f.samp_r2==0 & f2.samp_r2==1
	egen temp1 = max(year), by(samp_r2 c_id)
	replace temp1 = 0 if samp_r2!=1
	egen r2_year_end = max(temp1), by(country)
list country year r2_year_start r2_year_end if r2_year_start !=0

	drop temp1
	gen r3_year_start = 0 
	replace r3_year_start = year+1 if samp_r3==0 & f.samp_r3==0 & f2.samp_r3==1
	egen temp1 = max(year), by(samp_r3 c_id)
	replace temp1 = 0 if samp_r3!=1
	egen r3_year_end = max(temp1), by(country)
list country year r3_year_start r3_year_end if r3_year_start !=0

drop temp1
drop r1_year_start r1_year_end
drop r2_year_start r2_year_end
drop r3_year_start r3_year_end






/******************************************************************** 
* APPENDIX TABLE B1 - LIST OF OBSERVATIONS, highlighting FT=1 and MAJ=1
********************************************************************/

gen temp = 0 
replace temp = 1 if samp_r1==1 | samp_r2==1 | samp_r3==1
replace temp = 1 if f.samp_r1==1 | f.samp_r2==1 | f.samp_r3==1

list country year ft lt maj gfrac if temp==1, clean
* drop temp

	
	
	

	
	
	
	
/************************************************************
FIG. 1:  COUNTRY COVERAGE GRAPH 

NOTE: the creation of Fig. 1 collapses the dataset into 1 obs per year
To proceed past this section, one must clear stata and restart the do file from the top. 

The code identifies the year of the first non-missing country obs in each panel, 
then it sums countries by year, then it collapses by year, then it graphs cumulation across years. 
************************************************************/


/******************

gen c_first_ob_r1 = 0
replace c_first_ob_r1 = 1 if samp_r1==0 & f.samp_r1==1  	/* id 1st instance per country */
gen c_first_ob_r2 = 0
replace c_first_ob_r2 = 1 if samp_r2==0 & f.samp_r2==1
gen c_first_ob_r3 = 0
replace c_first_ob_r3 = 1 if samp_r3==0 & f.samp_r3==1 


list country year if c_first_ob_r1==1
list country year if c_first_ob_r2==1 			
list country year if c_first_ob_r3==1			 

egen y_n_r1 = sum(c_first_ob_r1), by(year) 		/* sum first instance by year */
egen y_n_r2 = sum(c_first_ob_r2), by(year)
egen y_n_r3 = sum(c_first_ob_r3), by(year)
egen y_id = seq(), by(year)
drop if year<1965								/* drop years per 1970 */
keep if y_id==1									/* keep only 1 obs per year */
sort year
tsset year


forvalues i =1970/2020 {
    replace y_n_r1 = y_n_r1 + l.y_n_r1 if year==`i'
	replace y_n_r2 = y_n_r2 + l.y_n_r2 if year==`i'
	replace y_n_r3 = y_n_r3 + l.y_n_r3 if year==`i'
  }

  
**** adjustment for countries that fall off at end
* drop off after 2016:  1 country (spain)
replace y_n_r1 = l.y_n_r1 - 1  if year==2017
replace y_n_r2 = l.y_n_r2 - 1  if year==2017
replace y_n_r3 = l.y_n_r3 - 1  if year==2017
* drop off after 2017:  2 countries (Germany, Japan)
replace y_n_r1 = l.y_n_r1 - 2  if year==2018
replace y_n_r2 = l.y_n_r2 - 2  if year==2018
replace y_n_r3 = l.y_n_r3 - 2  if year==2018
* drop off after 2018:  4-5 countries (Czech Republic, Italy, Slovenia, Sweden), plus Hungary for D2 & D3
replace y_n_r1 = l.y_n_r1 - 4  if year==2019
replace y_n_r2 = l.y_n_r2 - 5  if year==2019
replace y_n_r3 = l.y_n_r3 - 5  if year==2019




twoway (connected y_n_r2 year , msymbol(none) lcolor(black) lpattern(solid) lwidth(thick)) ///
(connected y_n_r1 year, msymbol(none) lwidth(thick) lpattern(dash_dot)) ///
(connected y_n_r3 year, msymbol(none) lcolor(gs8) lpattern(longdash) lwidth(thick)), ///
legend(ring(0) pos(11) col(1) order(2 1 3) lab(2 "D1") lab(1 "D2") lab(3 "D3")) ylabel(, angle(horizontal)) ///
xlabel(1970(10)2020) yline(0 5 10 15 20 25, lcolor(gs14)) xtitle("")



* END OF FIG. 1 SECTION
************************/

	
	
	
	





/******************************************
* APPENDIX C - TABLE C.1: SERIAL CORRELATION & MODEL TESTS - D3
******************************************/

*** IDENTIFY d3 SAMPLE FOR MODEL TESTS  - use ADLLDV2 to set sample for all regressions in this set
reg d3 l.d3 l2.d3 g1 u cpi bcrisis euro2 lt ft left nfr ///
l.g1 l.u l.cpi l.bcrisis l.euro2 l.lt l.ft l.left l.nfr if samp_r3==1

gen samp_sc_d3 = 0 
replace samp_sc_d3 = 1 if e(sample)


/* Does ECLDV2 provide the same results as ADL-LDV2? A: Yes 
reg d3 l.d3 l2.d3 d.lt d.ft d.left d.nfr d.g1 d.u d.d.cpi d.euro2 d.bcrisis ///
    l.lt l.ft l.left l.nfr l.g1 l.u l.cpi l.euro2 l.bcrisis  if samp_r3==1
*/
	
	

* 1 - FD political
reg d.d3 d.lt d.ft d.left d.nfr if samp_sc_d3==1
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag d.lt d.ft d.left d.nfr if samp_sc_d3==1
	* SC test result: 
drop resid*


* 2 - ADL political
reg d3 l.d3 lt ft left nfr l.lt l.ft l.left l.nfr if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 lt ft left nfr l.lt l.ft l.left l.nfr if samp_sc_d3==1
	* SC test result: p=.05
drop resid*


* 3 - ADLLDV2 political
reg d3 l.d3 l2.d3 lt ft left nfr l.lt l.ft l.left l.nfr if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 l2.d3 lt ft left nfr l.lt l.ft l.left l.nfr if samp_sc_d3==1
	* SC test result: 
drop resid*


* 4 - FD economic 
reg d.d3 d.g1 d.u d.d.cpi d.bcrisis d.euro2 if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag d.g1 d.u d.d.cpi d.bcrisis d.euro2  if samp_sc_d3==1
	* SC test result: 
drop resid*


* 5 - ADL economic
reg d3 l.d3 g1 u cpi bcrisis euro2 l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d1) alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 g1 u cpi bcrisis euro2 l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_sc_d3==1
	* SC test result: 


	
*** FIGURE C1: graph of residuals by panel=country & x=year 
twoway (scatter resid year) if samp_sc_d3==1, by(countryname, cols(4) ixtick note("") title(, fcolor(white))) /// 
xtitle("") ytitle("") ylabel(, angle(horizontal) grid) yline(0, lcolor(gs10)) ///
xscale(range(1970 2020)) xlabel(1970(20)2010)



drop resid*



* 6 - ADLLDV2 economic
reg d3 l.d3 l2.d3 g1 u cpi bcrisis euro2 l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 l2.d3 g1 u cpi bcrisis euro2 l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_sc_d3==1
	* SC test result: 
drop resid*


* 7 - ADL pol-econ
reg d3 l.d3 g1 u cpi bcrisis euro2 lt ft left nfr ///
l.g1 l.u l.cpi l.bcrisis l.euro2 l.lt l.ft l.left l.nfr if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 g1 u cpi bcrisis euro2 lt ft left nfr ///
l.g1 l.u l.cpi l.bcrisis l.euro2 l.lt l.ft l.left l.nfr if samp_sc_d3==1
	* SC test result: 	
drop resid*



* 8 - ADLLDV2 pol-econ
reg d3 l.d3 l2.d3 g1 u cpi bcrisis euro2 lt ft left nfr ///
l.g1 l.u l.cpi l.bcrisis l.euro2 l.lt l.ft l.left l.nfr if samp_sc_d3==1
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) ctitle(d3)  alpha(.05) symbol(*)	
	predict resid, r
	gen resid_lag = l.resid
reg resid resid_lag l.d3 l2.d3 g1 u cpi bcrisis euro2 lt ft left nfr ///
l.g1 l.u l.cpi l.bcrisis l.euro2 l.lt l.ft l.left l.nfr if samp_sc_d3==1
	* SC test result: 
drop resid*





	
	


/******************************************************************** 
* FIGURE 1 - LT effect kensities (half violin plots) - debt residuals 
********************************************************************/
* violinplot. Option "absolute" (not used) highlights differences in n obs

* COLUMN 1:  LT=1 vs. FT=LT=0
* residuals from regression 3.1
violinplot  res_r1 if samp_r1==1 & ft==0, /// 
over(neg_lt)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("LT=1" "FT=LT=0") ///
xsize(7) ///
xtitle("") /// 
xlabel(-10(5)10, labsize(vlarge)) ///
title("Residuals from regression 3.1 (D1)", size(vlarge)) 


* residuals from regression 3.2
violinplot  res_r2 if samp_r2==1 & ft==0, /// 
over(neg_lt)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("LT=1" "FT=LT=0") ///
xsize(7) ///
xlabel(-10(5)10, labsize(vlarge)) ///
title("Residuals from regression 3.2 (D2)", size(vlarge))


* residuals from regression 3.3
violinplot res_r3 if samp_r3==1 & ft==0, /// 
over(neg_lt)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("LT=1" "FT=LT=0") ///
xsize(7) ///
xlabel(-10(5)10, labsize(vlarge)) ///
title("Residuals from regression 3.3 (D3)", size(vlarge))




* COLUMN 2:  LT=1 by NMAJ 
* residuals from regression 3.1
violinplot  res_r1 if samp_r1==1 & lt==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("LT=1 residuals from regression 3.1 (D1)", size(vlarge)) 


* residuals from regression 3.2
violinplot  res_r2 if samp_r2==1 & lt==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("LT=1 residuals from regression 3.2 (D2)", size(vlarge)) 

list country year maj res_r2 if samp_r2==1 & lt==1 & res_r2>4
/* nmaj=1 outlier is Denmark 1982 */


* residuals from regression 3.3
violinplot  res_r3 if samp_r3==1 & lt==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("LT=1 residuals from regression 3.3 (D3)", size(vlarge)) 








/******************************************************
*  Appendix - TABLE E1: residuals, H-FTV, H-LT  
******************************************************/

*********
* residuals from reg 3.1
oneway res_r1 ft if samp_r1==1, tab 
ttest res_r1 if samp_r1==1 & ft==0, by(lt)
* compare median 
sum res_r1 if samp_r1==1 & ft==0 & lt==0, detail
sum res_r1 if samp_r1==1 & ft==0 & lt==1, detail


*********
* residuals from reg 3.2
oneway res_r2 ft if samp_r2==1, tab 
ttest res_r2 if samp_r2==1 & ft==0, by(lt)
* compare median 
sum res_r2 if samp_r2==1 & ft==0 & lt==0, detail
sum res_r2 if samp_r2==1 & ft==0 & lt==1, detail


*********
* residuals from reg 3.3
oneway res_r3 ft if samp_r3==1, tab 
ttest res_r3 if samp_r3==1 & ft==0, by(lt)
* compare median 
sum res_r3 if samp_r3==1 & ft==0 & lt==0, detail
sum res_r3 if samp_r3==1 & ft==0 & lt==1, detail



*********
*** MAJ==1
* reg 3.1, maj==1
oneway res_r1 ft if samp_r1==1 & maj==1, tab 
ttest res_r1 if samp_r1==1 & ft==0 & maj==1, by(lt)
* compare median 
sum res_r1 if samp_r1==1 & ft==0 & lt==0 & maj==1, detail
sum res_r1 if samp_r1==1 & ft==0 & lt==1 & maj==1, detail

* reg 3.2, maj==1
oneway res_r2 ft if samp_r2==1 & maj==1, tab 
ttest res_r2 if samp_r2==1 & ft==0 & maj==1, by(lt)
* compare median 
sum res_r2 if samp_r2==1 & ft==0 & lt==0 & maj==1, detail
sum res_r2 if samp_r2==1 & ft==0 & lt==1 & maj==1, detail

* reg 3.3, maj==1
oneway res_r3 ft if samp_r3==1 & maj==1, tab 
ttest res_r3 if samp_r3==1 & ft==0 & maj==1, by(lt)
* compare median 
sum res_r3 if samp_r3==1 & ft==0 & lt==0 & maj==1, detail
sum res_r3 if samp_r3==1 & ft==0 & lt==1 & maj==1, detail



*********
*** MAJ==0
* reg 3.1, maj==0
oneway res_r1 ft if samp_r1==1 & maj==0, tab 
ttest res_r1 if samp_r1==1 & ft==0 & maj==0, by(lt)
* compare median 
sum res_r1 if samp_r1==1 & ft==0 & lt==0 & maj==0, detail
sum res_r1 if samp_r1==1 & ft==0 & lt==1 & maj==0, detail

* reg 3.2, maj==0
oneway res_r2 ft if samp_r2==1 & maj==0, tab 
ttest res_r2 if samp_r2==1 & ft==0 & maj==0, by(lt)
* compare median 
sum res_r2 if samp_r2==1 & ft==0 & lt==0 & maj==0, detail
sum res_r2 if samp_r2==1 & ft==0 & lt==1 & maj==0, detail

* reg 3.3, maj==0
oneway res_r3 ft if samp_r3==1 & maj==0, tab 
ttest res_r3 if samp_r3==1 & ft==0 & maj==0, by(lt)
* compare median 
sum res_r3 if samp_r3==1 & ft==0 & lt==0 & maj==0, detail
sum res_r3 if samp_r3==1 & ft==0 & lt==1 & maj==0, detail








/******************************************************************** 
* FIGURE 2 - FTV kensities (half violin plots) - debt residuals 
********************************************************************/ 
* COLUMN 1:  FT=1 vs. FT=0
* residuals from regression 3.1
violinplot  res_r1 if samp_r1==1, /// 
over(neg_ft)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)   /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("FT=1" "FT=0") ///
xsize(7) ///
xlabel(-10(5)15, labsize(vlarge)) ///
title("Residuals from regression 3.1 (D1)", size(vlarge)) 

* residuals from regression 3.2
violinplot  res_r2 if samp_r2==1, /// 
over(neg_ft)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)   /// 
median(type(line) lwidth(medthick)) ///
xtitle("") xlabel( , labsize(large)) /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("FT=1" "FT=0") ///
xsize(7) ///
xlabel(-10(5)15, labsize(vlarge)) ///
title("Residuals from regression 3.2 (D2)", size(vlarge)) 

* residuals from regression 3.3
violinplot  res_r3 if samp_r3==1, /// 
over(neg_ft)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)   /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("FT=1" "FT=0") ///
xsize(7) ///
xlabel(-10(5)15, labsize(vlarge)) ///
title("Residuals from regression 3.3 (D3)", size(vlarge)) 




* COLUMN 2:  FT by maj 
* residuals from regression 3.1
violinplot  res_r1 if samp_r1==1 & ft==1, /// 
over(nmaj) left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)15, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.1 (D1)", size(vlarge)) 

* residuals from regression 3.2
violinplot  res_r2 if samp_r2==1 & ft==1, /// 
over(nmaj) left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)15, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.2 (D2)", size(vlarge)) 

* residuals from regression 3.3
violinplot  res_r3 if samp_r3==1 & ft==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)15, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.3 (D3)", size(vlarge)) 












********************************************************
/*************************************************
** Appendix Fig. E1: FTV by MAJ for MAJ-variation countries
*************************************************/
********************************************************

	gen temp1 = maj
	replace temp1 = . if samp_r1==0
	egen temp2 = mean(temp1), by(country)
	gen samp_majvar_r1 = 1
	replace samp_majvar_r1 = 0 if temp2==0
	replace samp_majvar_r1 = 0 if temp2==1	
	replace samp_majvar_r1 = 0 if samp_r1==0
	

* residuals from regression 3.1
violinplot  res_r1 if samp_majvar_r1==1 & ft==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.1 (D1)", size(vlarge)) 


drop temp* 
	gen temp1 = maj
	replace temp1 = . if samp_r2==0
	egen temp2 = mean(temp1), by(country)
	gen samp_majvar_r2 = 1
	replace samp_majvar_r2 = 0 if temp2==0
	replace samp_majvar_r2 = 0 if temp2==1	
	replace samp_majvar_r2 = 0 if samp_r2==0
	

* residuals from regression 3.2
violinplot  res_r2 if samp_majvar_r2==1 & ft==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.2 (D2)", size(vlarge)) 


drop temp* 
	gen temp1 = maj
	replace temp1 = . if samp_r3==0
	egen temp2 = mean(temp1), by(country)
	gen samp_majvar_r3 = 1
	replace samp_majvar_r3 = 0 if temp2==0
	replace samp_majvar_r3 = 0 if temp2==1	
	replace samp_majvar_r3 = 0 if samp_r3==0
	

* residuals from regression 3.3
violinplot  res_r3 if samp_majvar_r3==1 & ft==1, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("FT=1 residuals from regression 3.3 (D3)", size(vlarge)) 

drop temp*






		
		
/******************************************************
*  Table 4:  main regressions 
******************************************************/
/*  three models
1. ft and lt, no vp variables (because they have differential effects on FT vs. LT) 
2. add nmaj & interactions with ft & lt 
3. add gfr & interactions with ft & lt 
*/

**** 4.1:  D1 nmaj 
reg d.d1 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)
	
**** 4.2: D2 nmaj 
reg d.d2 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

**** 4.3: D3 nmaj 
reg d.d3 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)


**** nmaj, & interactions with lt and ft 
**** 4.4: D1 
reg d.d1 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d1 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
	
	
**** 4.5:  D2 
reg d.d2 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d2 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	
**** 4.6:  D3 
reg d.d3 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d3 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)



**** add gf, & interactions with lt and ft 
**** 4.7: D1 
reg d.d1 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d1 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*

	
	
**** 4.8:  D2 
reg d.d2 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d2 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*


	
**** 4.9:  D3 
reg d.d3 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d3 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*









	


		
/******************************************************
*  Appendix Table E2:  ADLLDV2 
******************************************************/

**** e2.1:  D1 nmaj (not shown)
reg d.d1 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l2.d1 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)
	
**** e2.2: D2 nmaj  (not shown)
reg d.d2 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l2.d2 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

**** e2.3: D3 nmaj  (not shown)
reg d.d3 d.lt d.ft d.left d.nfrules d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l2.d3 l.lt l.ft l.left l.nfrules l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)


**** nmaj, & interactions with lt and ft 
**** e2.4: D1 
reg d.d1 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d1 l2.d1 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
	
	
**** e2.5:  D2 
reg d.d2 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d2 l2.d2 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	
**** e2.6:  D3 
reg d.d3 d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d3 l2.d3 l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)



**** add gf, & interactions with lt and ft 
**** e2.7: D1 
reg d.d1 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l2.d1 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d1 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d1 l2.d1 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*

	
	
**** e2.8:  D2 
reg d.d2 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l2.d2 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d2 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d2 l2.d2 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*


	
**** e2.9:  D3 
reg d.d3 d.ft d.lt d.nmaj d.gfrac d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l2.d3 l.ft l.lt l.nmaj l.gfrac l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
reg d.d3 d.lt d.i_nmaj_lt d.i_gfr2_lt d.ft d.i_nmaj_ft d.i_gfr2_ft d.nmaj d.gfr2 ///
	d.left d.nfr d.g1 d.u d.cpi d.bcrisis d.euro2 ///
    l.d3 l2.d3 l.lt l.i_nmaj_lt l.i_gfr2_lt l.ft l.i_nmaj_ft l.i_gfr2_ft l.nmaj l.gfr2 ///
	l.left l.nfr l.g1 l.u l.cpi l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)


drop temp1 gfr2 i_gfr*







/******************************************************
* Appendix Table E3 - AR(1) regressions 
******************************************************/

**** E3.1:  D1 nmaj  (not shown)
xtregar d1 lt ft left nfrules g1 u1 cpi bcrisis euro2 if samp_r1==1,fe 
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)
	
**** E3.2: D2 nmaj  (not shown)
xtregar d2 lt ft left nfrules g1 u1 cpi bcrisis euro2 if samp_r2==1,fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

**** E3.3: D3 nmaj  (not shown)
xtregar d3 lt ft left nfrules g1 u1 cpi bcrisis euro2 if samp_r3==1,fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)



**** nmaj, & interactions with lt and ft 
**** E3.4: D1 
xtregar d1 lt ft left nfrules g1 u1 cpi bcrisis euro2 ///
i_nmaj_lt i_nmaj_ft nmaj if samp_r1==1, fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
	
	
**** E3.5:  D2 
xtregar d2 lt ft left nfrules g1 u1 cpi bcrisis euro2 ///
i_nmaj_lt i_nmaj_ft nmaj if samp_r2==1, fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

* exclude denmark pre-1983
gen temp = 1
replace temp = 0 if country=="Denmark" & year<1983
xtregar d2 lt ft left nfrules g1 u1 cpi bcrisis euro2 ///
i_nmaj_lt i_nmaj_ft nmaj if samp_r2==1 & temp==1, fe 


**** E3.6:  D3 
xtregar d3 lt ft left nfrules g1 u1 cpi bcrisis euro2 ///
i_nmaj_lt i_nmaj_ft nmaj if samp_r3==1, fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	
	
**** add gf, & interactions with lt and ft 
**** E3.7: D1 
xtregar d1 ft lt nmaj gfrac left nfr g1 u1 cpi bcrisis euro2 if samp_r1==1, fe
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
xtregar d1 lt i_nmaj_lt i_gfr2_lt ft i_nmaj_ft i_gfr2_ft nmaj gfr2 ///
	left nfr g1 u1 cpi bcrisis euro2 if samp_r1==1, fe
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*

	
**** E3.8:  D2 
xtregar d2 ft lt nmaj gfrac left nfr g1 u1 cpi bcrisis euro2 if samp_r2==1, fe
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
xtregar d2 lt i_nmaj_lt i_gfr2_lt ft i_nmaj_ft i_gfr2_ft nmaj gfr2 ///
	left nfr g1 u1 cpi bcrisis euro2 if samp_r2==1, fe
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

* exclude denmark pre-1983
replace temp = 1
replace temp = 0 if country=="Denmark" & year<1983
xtregar d2 lt i_nmaj_lt i_gfr2_lt ft i_nmaj_ft i_gfr2_ft nmaj gfr2 ///
	left nfr g1 u1 cpi bcrisis euro2 if samp_r2==1 & temp==1, fe
drop temp1 gfr2 i_gfr*


**** E3.9:  D3 
xtregar d1 ft lt nmaj gfrac left nfr g1 u1 cpi bcrisis euro2 if samp_r3==1, fe
	gen temp1 = gfrac
	replace temp1 = . if e(sample)==0
	egen gfr2 = sd(temp1)
	replace gfr2 = gfrac/gfr2 
	gen i_gfr2_lt = gfr2*lt
	gen i_gfr2_ft = gfr2*ft
xtregar d1 lt i_nmaj_lt i_gfr2_lt ft i_nmaj_ft i_gfr2_ft nmaj gfr2 ///
	left nfr g1 u1 cpi bcrisis euro2 if samp_r3==1, fe
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
drop temp1 gfr2 i_gfr*





	
	

	
	
	





/********************************************************************************
******* APPENDIX F: SHORT V FULL TERMS
********************************************************************************/
	
tab country nyears_term

egen max_years = max(nyears_term), by(country)
replace max_years = 5 if max_years==6
replace max_years = 3 if country=="Australia"
replace max_years = 4 if country=="Greece"

gen short = 0 
replace short = 1 if nyears_term<max_years 
tab short parl if samp_r3==1
replace short = 0 if country=="Uruguay"
tab country short 





/********************************************************************************
**  TABLE F1: MAIN REGRESSIONS 4.4-4.6 WITH NYEARS_TERM & SHORT 
********************************************************************************/
	
**** ADD NYEARS_TERM 
**** e4.4: D1 
reg d.d1 d.nyears_term d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d1 l.nyears_term l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)
	
	
**** e4.5:  D2 
reg d.d2 d.nyears_term d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d2 l.nyears_term l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	
**** e4.6:  D3 
reg d.d3 d.nyears_term d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d3 l.nyears_term l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)


**** ADD SHORT
**** e4.4: D1 
reg d.d1 d.short d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d1 l.short l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r1==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)
	
	
**** e4.5:  D2 
reg d.d2 d.short d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d2 l.short l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r2==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	
**** e4.6:  D3 
reg d.d3 d.short d.lt d.i_nmaj_lt d.ft d.i_nmaj_ft d.nmaj d.left d.nfr d.g1 d.cpi d.u1 d.bcrisis d.euro2 ///
    l.d3 l.short l.lt l.i_nmaj_lt l.ft l.i_nmaj_ft l.nmaj l.left l.nfr l.g1 l.cpi  l.u1 l.bcrisis l.euro2 if samp_r3==1, robust 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)







/********************************************************************************
**  TABLE F2: SHORT-TERMS, TERM-TYPE CORRELATES 
********************************************************************************/

gen negd = 0 
replace negd = 1 if d3<0

gen ft_negd = 0 
replace ft_negd = 1 if ft==1 & d3<0

gen lt_negd = 0 
replace lt_negd = 1 if lt==1 & d3<0

gen ltmaj = 0 
replace ltmaj = 1 if lt==1 & maj==1 

gen ftmaj = 0 
replace ftmaj = 1 if ft==1 & maj==1 


	**** nyears_term 
	xtreg nyears_term ft lt if samp_r3==1 & parl==1, fe 
	outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)

	xtreg nyears_term ft negd ft_negd lt if samp_r3==1 & parl==1, fe 
	outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

	xtreg nyears_term ft negd ft_negd lt maj ltmaj if samp_r3==1 & parl==1, fe 
	outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)



* Table F2:  xtlogit 
xtlogit short ft lt if samp_r3==1 & parl==1, fe 
outreg2 using debt_dynamics_output, replace word dec(2) bdec(2) alpha(.10) symbol(*)

xtlogit short ft negd ft_negd lt lt_negd if samp_r3==1 & parl==1, fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)

xtlogit short ft lt maj ltmaj ftmaj if samp_r3==1 & parl==1, fe 
outreg2 using debt_dynamics_output, append word dec(2) bdec(2) alpha(.10) symbol(*)




**** FIGURE F2 
* D3 residuals, kdensities, SHORT-TERMS removed 
* LT=1 vs. FT=LT=0
violinplot  res_r3 if samp_r3==1 & short==0 & ft==0, /// 
over(neg_lt)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)   /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("LT=1" "FT=LT=0") ///
xsize(7) ///
xlabel(-10(5)10, labsize(vlarge)) ///
title("Last-term effect", size(vlarge)) 


* LT=1, by NMAJ 
violinplot  res_r3 if samp_r3==1 & lt==1 & short==0, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("Last terms and majority government", size(vlarge)) 

* FT vs. FT=0
violinplot  res_r3 if samp_r3==1 & short==0, /// 
over(neg_ft)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)   /// 
median(type(line) lwidth(medthick)) ///
xtitle("")  /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("FT=1" "FT=0") ///
xsize(7) ///
xlabel(-10(5)10, labsize(vlarge)) ///
title("First-term vs. Non-first-term trajectories", size(vlarge)) 


* FT=1, by NMAJ 
violinplot  res_r3 if samp_r3==1 & ft==1 & short==0, /// 
over(nmaj)  left overlay colors(black gs7) ///
fill(select(1) color(%15)) line(lwidth(thick)) key(line)  /// 
median(type(line) lwidth(medthick)) ///
xtitle("") /// 
legend(ring(0) pos(2) col(1) size(vlarge)) ///
olabels("MAJ=1" "MAJ=0") ///
xlabel(-10(5)10, labsize(vlarge)) ///
xsize(7) ///
title("First terms and majority government", size(vlarge)) 









	
	
	
	
	

********************************************************************************
********************************************************************************
*******  RUNS & IBCs 
********************************************************************************
********************************************************************************

/***************************
* CREATE RUNS DATA  
THIS SECTION MAKES MANY NEW VARIABLES BEFORE MAKING TABLE 7 & FIGURE 4.
Here are some of the variables. Others are described below. 

run_id 			= id for run (=id_term for first term in the run)
run_count 		= counting terms in run  
run_complete 	= dummy if the run is complete (first term and last term are in the dataset)
r1_run 			= dummy if run is complete ("1" signifies use of reg. 3.1 regression sample)
r1_lt 			= dummy that identifies the last term in the run ("1" signifies use of reg. 3.1 regression sample)
r1_low_res_ft 		= dummy if lowest residual is in first term (=IBC) ("1" signifies use of reg. 3.1 regression sample)
r1_high_res_lt 		= dummy if highest residual is in last term  ("1" signifies use of reg. 3.1 regression sample)
r1_res_ft 			= the residual in first term ("1" signifies use of reg. 3.1 regression sample)
r1_res_lt 			= the residual in last term (=res_r1 or whichever regression) ("1" signifies use of reg. 3.1 regression sample)
*/



egen ctry_id = group(countryname)


*************
*** RUN_COUNT - stops at 7 (all terms 7+ are coded = 7, but only Belgium fits that, and it is dropped)
gen run_count = 0 	/* run_count  */
replace run_count = 1 if ft==1  		/* ft in run */
replace run_count = 2 if ft==0 & l.run_count==1 & ctry_id==l.ctry_id   /* ft+1 in run */
replace run_count = 3 if ft==0 & l.ft==0 & l.run_count==2  & ctry_id==l.ctry_id  		/* ft+2 in run */
replace run_count = 4 if ft==0 & l.ft==0 & l2.ft==0 & l.run_count==3 & ctry_id==l.ctry_id  		/* ft+3 */
replace run_count = 5 if ft==0 & l.ft==0 & l2.ft==0 & l3.ft==0 & l.run_count==4 & ctry_id==l.ctry_id  
replace run_count = 6 if ft==0 & l.ft==0 & l2.ft==0 & l3.ft==0 & l4.ft==0 & l.run_count==5 & ctry_id==l.ctry_i  
replace run_count = 7 if ft==0 & l.ft==0 & l2.ft==0 & l3.ft==0 & l4.ft==0 & l5.ft==0  & ctry_id==l.ctry_id  
replace run_count = . if run_count==0
* note that run_count is also modified after RUN_RANGE is created



*************
*** RUN_RANGE: dummy that begins with first FT=1 in panel, ends with last LT=1	
* find first FT=1 in panel... everything below it is run_complete=0
gen temp1 = id_term - 100*ft
egen temp4= min(temp1), by(ctry_id)
replace temp4 = 0 if temp4 != temp1
replace temp4 = id_term if temp4 == temp1
egen first_ft = max(temp4), by(ctry_id)

* find last LT=1 in panel
replace temp1 = id_term + 100*lt
egen temp5 = max(temp1), by(ctry_id)
replace temp5 = 0 if temp5 != temp1
replace temp5 = id_term if temp5 == temp1
egen last_lt = max(temp5), by(ctry_id)

* RUN_RANGE = dummy for complete runs in panel
gen run_range = 1
replace run_range = 0 if id_term<first_ft 
replace run_range = 0 if id_term>last_lt
	list country year id_term ft first_ft lt last_lt run_range
replace run_range = 0 if country=="Belgium"	 /* no complete runs */
replace run_range = 0 if country=="Estonia"  /* no complete runs */
* replace run_count = . if run_range==0
	list country year ft lt run_range run_count
	
* UNFINISHED_RUN = 1 if the run started but not finished in dataset (so, only at end of panels)	
gen unfinished_run = 0 
replace unfinished_run = 1 if id_term>last_lt
replace unfinished_run = 0 if ft==1 & f.ft==1 & unfinished_run==1 & ctry_id==f.ctry_id 
replace unfinished_run = 0 if ft==1 & unfinished_run==1 & ctry_id!=f.ctry_id 
* Aus, Germany, Hungary, Israel, Japan, Netherlands, UK
* replace run_range= 1 if unfinished_run==1

drop temp*
drop first_ft last_lt 



*************
*** RUN_ID_FT - send id_term of first term through run
gen run_id_ft = -99
replace run_id_ft = id_term if ft==1
replace run_id_ft = l.id_term if run_count==2
replace run_id_ft = l2.id_term if run_count==3
replace run_id_ft = l3.id_term if run_count==4
replace run_id_ft = l4.id_term if run_count==5
replace run_id_ft = l5.id_term if run_count==6
replace run_id_ft = l6.id_term if run_count==7
replace run_id_ft = . if run_id_ft==-99



*************
*** RUN_ID_LT send id_term of last term through run
gen run_id_lt = -99
replace run_id_lt = id_term if lt==1
replace run_id_lt = f.id_term if f.lt==1    &  run_id_ft==f.run_id_ft
replace run_id_lt = f2.id_term if f2.lt==1  &  run_id_ft==f2.run_id_ft
replace run_id_lt = f3.id_term if f3.lt==1  &  run_id_ft==f3.run_id_ft
replace run_id_lt = f4.id_term if f4.lt==1  &  run_id_ft==f4.run_id_ft
replace run_id_lt = f5.id_term if f5.lt==1  &  run_id_ft==f5.run_id_ft
replace run_id_lt = f6.id_term if f6.lt==1  &  run_id_ft==f6.run_id_ft

replace run_id_lt = . if run_id_lt==-99
replace run_id_ft = . if run_id_lt==.
replace run_id_lt = . if run_id_ft==.

list country year ft lt run_range run_count run_id_ft run_id_lt 



*************
*** RUN_TERMS = number of terms in run
egen run_terms = max(run_count), by(run_id_ft)
replace run_terms = . if run_id_ft==.

list country year ft lt run_range run_count run_id_ft run_id_lt run_terms



*************
*** RUNS_Total = number of terms in run = 194
egen runs_total = group(run_id_ft)


*************
* YEAR_FT_END = year that first term in run ended  
egen year_ft_end = min(year), by(run_id_ft)
replace year_ft_end = . if run_range==0
replace year_ft_end = . if run_id_ft==.

list country year ft lt run_range run_count run_id_ft run_id_lt run_terms year_ft_end




****************************
*** Runs - Regression 1 
*** RES_FT_R1 = the residual in first term, regression 1
gen temp1 = -100
replace temp1 = res_r1 if ft==1 

egen res_ft_r1  = max(temp1), by(run_id_ft) 
replace res_ft_r1 = . if run_range==0
replace res_ft_r1 = . if run_id_ft==.
replace res_ft_r1 = . if res_ft_r1==-100

*** RUN_COMPLETE_R1 = dummy, =1 if run complete (there are residuals for ft and lt)
gen run_complete_r1 = 0
replace run_complete_r1 = 1 if res_r1!=. & lt==1 & run_range==1 & res_ft_r1!=.
replace run_complete_r1 = 0 if res_ft_r1==-100
egen temp2 = max(run_complete_r1), by(run_id_lt)
replace run_complete_r1=1 if temp2==1
drop temp1 temp2   

*** MIN_RES_R1 = min residual in run 
egen min_res_r1 = min(res_r1), by(run_id_ft) 
replace min_res_r1 = . if run_range==0
replace min_res_r1 = . if run_id_ft==.
replace min_res_r1 = . if run_complete_r1==0

*** LOW_RES_FT_R1 = dummy if lowest residual is in first term (=IBC)
gen low_res_ft_r1 = 0
replace low_res_ft_r1 = 1 if min_res_r1==res_ft_r1
replace low_res_ft_r1 = . if run_range==0
replace low_res_ft_r1 = . if run_id_ft==0


tab low_res_ft_r1 run_count if lt==1 & run_complete_r1==1 & lt==1





****************************
*** Runs - Regression 2 
*** RES_FT_r2 = the residual in first term, regression 2
gen temp1 = -100
replace temp1 = res_r2 if ft==1 

egen res_ft_r2  = max(temp1), by(run_id_ft) 
replace res_ft_r2 = . if run_range==0
replace res_ft_r2 = . if run_id_ft==.
replace res_ft_r2 = . if res_ft_r2==-100

*** RUN_COMPLETE_r2 = dummy, =1 if run complete (there are residuals for ft and lt)
gen run_complete_r2 = 0
replace run_complete_r2 = 1 if res_r2!=. & lt==1 & run_range==1 & res_ft_r2!=.
replace run_complete_r2 = 0 if res_ft_r2==-100
egen temp2 = max(run_complete_r2), by(run_id_lt)
replace run_complete_r2=1 if temp2==1
drop temp1 temp2   

*** MIN_RES_r2 = min residual in run 
egen min_res_r2 = min(res_r2), by(run_id_ft) 
replace min_res_r2 = . if run_range==0
replace min_res_r2 = . if run_id_ft==.
replace min_res_r2 = . if run_complete_r2==0

*** LOW_RES_FT_r2 = dummy if lowest residual is in first term (=IBC)
gen low_res_ft_r2 = 0
replace low_res_ft_r2 = 1 if min_res_r2==res_ft_r2
replace low_res_ft_r2 = . if run_range==0
replace low_res_ft_r2 = . if run_id_ft==0

tab low_res_ft_r2 run_count if lt==1 & run_complete_r2==1 & lt==1




****************************
*** Runs - Regression 3 
*** RES_FT_R3 = the residual in first term, regression 3
gen temp1 = -100
replace temp1 = res_r3 if ft==1 

egen res_ft_r3  = max(temp1), by(run_id_ft) 
replace res_ft_r3 = . if run_range==0
replace res_ft_r3 = . if run_id_ft==.
replace res_ft_r3 = . if res_ft_r3==-100

*** RUN_COMPLETE_r3 = dummy, =1 if run complete (there are residuals for ft and lt)
gen run_complete_r3 = 0
replace run_complete_r3 = 1 if res_r3!=. & lt==1 & run_range==1 & res_ft_r3!=.
replace run_complete_r3 = 0 if res_ft_r3==-100
egen temp2 = max(run_complete_r3), by(run_id_lt)
replace run_complete_r3=1 if temp2==1
drop temp1 temp2   

*** MIN_RES_r3 = min residual in run 
egen min_res_r3 = min(res_r3), by(run_id_ft) 
replace min_res_r3 = . if run_range==0
replace min_res_r3 = . if run_id_ft==.
replace min_res_r3 = . if run_complete_r3==0

*** LOW_RES_FT_r3 = dummy if lowest residual is in first term (=IBC)
gen low_res_ft_r3 = 0
replace low_res_ft_r3 = 1 if min_res_r3==res_ft_r3
replace low_res_ft_r3 = . if run_range==0
replace low_res_ft_r3 = . if run_id_ft==0

tab low_res_ft_r3 run_count if lt==1 & run_complete_r3==1 & lt==1









*************
** RUN_MAJ_EVERY_TERM = ALL TERMS IN RUN ARE MAJ=1
egen run_maj_avg= mean(maj), by(run_id_lt)

gen run_maj_every_term = 0 
replace run_maj_every_term = 1 if run_maj_avg==1 

list country year ft lt run_terms maj nmaj if samp_r3==1 & run_maj_every_term==1 & run_range==1


** RUN_MAJ_IN_RUN = 1 if there's at least 1 MAJ IN RUN 
gen run_maj_in_run = 0 
replace run_maj_in_run = 1 if run_maj_avg>0

 
list country year ft lt run_terms maj nmaj if run_maj_in_run==1 & samp_r3==1 & run_maj_every_term==0 & run_range==1
* Aus83 1 maj, then 2 nmaj
* Aus07 1 maj, then 3 nmaj
* Aus13 1 maj, then 1 nmaj
* Can06 3 maj, then 1 nmaj
* Can15 2 nmaj, then 1 maj
* Jap09 3 nmaj, then 1 maj
* NZ99 2 maj, then 1 nmaj
* ESP96 2 maj, then 2 nmaj
* ESP04 1 nmaj, then 1 maj

drop run_maj_avg



** END of making new runs variables
***********************************************************************************














**************************************************************************************
**************************************************************************************
* TABLE 5
**************************************************************************************
**************************************************************************************
* run_maj_every_term=1
tab low_res_ft_r1 if lt==1 & run_complete_r1==1 & run_maj_every_term==1 & samp_r1==1
tab low_res_ft_r2 if lt==1 & run_complete_r2==1 & run_maj_every_term==1 & samp_r2==1 
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_every_term==1 & samp_r3==1 

tab run_terms if lt==1 & run_complete_r1==1 & run_maj_every_term==1 & samp_r1==1
tab run_terms if lt==1 & run_complete_r2==1 & run_maj_every_term==1 & samp_r2==1
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_every_term==1 & samp_r3==1

* run_maj_in_run=1 & run_maj_every_term=0
tab low_res_ft_r1 if lt==1 & run_complete_r1==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r1==1
tab low_res_ft_r2 if lt==1 & run_complete_r2==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r2==1 
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r3==1 

tab run_terms if lt==1 & run_complete_r1==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r1==1
tab run_terms if lt==1 & run_complete_r2==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r2==1
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & run_maj_every_term==0 & samp_r3==1

* run_maj_in_run=1
tab low_res_ft_r1 if lt==1 & run_complete_r1==1 & run_maj_in_run==1 & samp_r1==1
tab low_res_ft_r2 if lt==1 & run_complete_r2==1 & run_maj_in_run==1 & samp_r2==1 
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1 

tab run_terms if lt==1 & run_complete_r1==1 & run_maj_in_run==1 & samp_r1==1
tab run_terms if lt==1 & run_complete_r2==1 & run_maj_in_run==1 & samp_r2==1
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1

* run_maj_in_run=0
tab low_res_ft_r1 if lt==1 & run_complete_r1==1 & run_maj_in_run!=1 & samp_r1==1
tab low_res_ft_r2 if lt==1 & run_complete_r2==1 & run_maj_in_run!=1 & samp_r2==1 
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_in_run!=1 & samp_r3==1 

tab run_terms if lt==1 & run_complete_r1==1 & run_maj_in_run!=1 & samp_r1==1
tab run_terms if lt==1 & run_complete_r2==1 & run_maj_in_run!=1 & samp_r2==1
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_in_run!=1 & samp_r3==1






*** Runs that straddled global financial crisis
* run_maj_in_run=1: 3 of 3 are IBCs 
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1 & year>2008 & year_ft_end<2008
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1 & year>2008 & year_ft_end<2008
list country year if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1 & year>2008 & year_ft_end<2008

* maj=0: 33% are IBCs:  2 of 4 are IBCs  
tab low_res_ft_r3 if lt==1 & run_complete_r3==1 & run_maj_in_run!=1 & samp_r3==1  & year>2008 & year_ft_end<2008
tab run_terms if lt==1 & run_complete_r3==1 & run_maj_in_run!=1 & samp_r3==1 & year>2008 & year_ft_end<2008
list country year if lt==1 & run_complete_r3==1 & run_maj_in_run==0 & samp_r3==1 & year>2008 & year_ft_end<2008





**********************************
**** Table 5, continued: Amplitudes within IBCs

* last-term residual minus first-term residual
gen diff_res_r1 = (res_r1 - res_ft_r1) 
gen diff_res_r2 = (res_r2 - res_ft_r2) 
gen diff_res_r3 = (res_r3 - res_ft_r3) 


*** compare group medians in IBCs by run type (maj or not) 
* r1 ibcs 
* run_maj_every_term=1
sum diff_res_r1 if run_maj_every_term==1 & low_res_ft_r1==1 & lt==1 & samp_r1==1 & run_complete_r1==1, detail 
* run_maj_in_run=1 & run_maj_every_term=0
sum diff_res_r1 if run_maj_in_run==1 & run_maj_every_term==0 & low_res_ft_r1==1 & lt==1 & samp_r1==1 & run_complete_r1==1, detail 
* run_maj_in_run=1
sum diff_res_r1 if run_maj_in_run==1 & low_res_ft_r1==1 & lt==1 & samp_r1==1 & run_complete_r1==1, detail 
* maj=0
sum diff_res_r1 if run_maj_in_run!=1 & low_res_ft_r1==1 & lt==1 & samp_r1==1 & run_complete_r1==1, detail 


* r2 ibcs 
* run_maj_every_term=1
sum diff_res_r2 if run_maj_every_term==1 & low_res_ft_r2==1 & lt==1 & samp_r2==1 & run_complete_r2==1, detail 
* run_maj_in_run=1 & run_maj_every_term=0
sum diff_res_r2 if run_maj_in_run==1 & run_maj_every_term==0 & low_res_ft_r2==1 & lt==1 & samp_r2==1 & run_complete_r2==1, detail 
* run_maj_in_run=1
sum diff_res_r2 if run_maj_in_run==1 & low_res_ft_r2==1 & lt==1 & samp_r2==1 & run_complete_r2==1, detail 
* maj=0
sum diff_res_r2 if run_maj_in_run!=1 & low_res_ft_r2==1 & lt==1 & samp_r2==1 & run_complete_r2==1, detail 


* r3 ibcs, run_maj_in_run=1 vs. maj=0
* run_maj_every_term=1
sum diff_res_r3 if run_maj_every_term==1 & low_res_ft_r3==1 & lt==1 & samp_r3==1 & run_complete_r3==1, detail 
* run_maj_in_run=1 & run_maj_every_term=0
sum diff_res_r3 if run_maj_in_run==1 & run_maj_every_term==0 & low_res_ft_r3==1 & lt==1 & samp_r3==1 & run_complete_r3==1, detail 
* run_maj_in_run=1
sum diff_res_r3 if run_maj_in_run==1 & low_res_ft_r3==1 & lt==1 & samp_r3==1 & run_complete_r3==1, detail 
* maj=0
sum diff_res_r3 if run_maj_in_run!=1 & low_res_ft_r3==1 & lt==1 & samp_r3==1 & run_complete_r3==1, detail 
	




* NZ99 run and CAN06 run both had austerity programs; both were IBC=0; and both went from MAJ to NMAJ. That is, the austerity didn't cause them to lose power, but they lost their majorities (and then lost power)
* AUS83 is similar

* partial-maj runs: 
* if the run starts with maj then switches, the govt could be hamstrung in ability to increase deficit
	* IBC=0: NZ99, CAN06, AUS83, ESP96 - all four have 2nd term with smallest residual 
	* IBC=1: AUS07, CAN15
	* FT Expansion: AUS13
* if the run starts with nmaj then switches, deficit growth is more likely
	* IBC=1: JAP09, ESP04


**************************************************************************************
**************************************************************************************
* FIGURE 4
**************************************************************************************
**************************************************************************************

gen graph_range3 = res_r3
replace graph_range3 = -8 if res_r3<0
replace graph_range3 = 8 if res_r3>0 

gen xftres = res_ft_r3 
gen yltres = res_r3



***** FIG 4 
** r3, run_maj_in_run==1
twoway (scatter ylt xft if lt==1 & run_complete_r3==1 & run_maj_in_run==0  & samp_r3==1, ///
msym(none) mlabel(run_terms) mlabsize(medlarge) mlabpos(0) mlabcolor(gs10)) ///
(scatter ylt xft if lt==1 & run_complete_r3==1 & run_maj_in_run==1 & samp_r3==1, ///
msym(none) mlabel(run_terms) mlabsize(medlarge) mlabpos(0) mlabcolor(black)) ///
(function y = x, ra(graph_range3) clpat(solid) lcolor(gs12)) , ///
yline(-8 -6 -2 2 6 8, lcolor(gs15)) ///
xline(-8 -6 -2 2 6 8, lcolor(gs15)) ///
yline(-4 4, lcolor(gs13)) ///
xline(-4 4, lcolor(gs13)) ///
yline(0, lcolor(gs5)) xline(0, lcolor(gs5)) ///
yscale(r(-8 8)) xscale(r(-8 8)) ///
ylabel(-8(4)8, angle(horizontal) labsize(medlarge)) ///
xlabel(-8(4)8, labsize(medlarge)) aspectratio(1) legend(off) ///
xtitle("First term residual", size(medlarge)) ytitle("Last term residual", size(medlarge)) 




* Q: how many runs in chart?  A: 45
list country year ylt xft if lt==1 & run_complete_r3==1 & samp_r3==1

* Q: last-term residuals - how many >0  A: 27 (60%)  
list country year ylt if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt>0

* Q: run_maj_in_run=1 runs: how many?   A: 18
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_in_run==1  & samp_r3==1 

* Q: run_maj_in_run=1 runs: how many with y>0? A: 13 (72%)
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_in_run==1  & samp_r3==1 & ylt>0

* Q: run_maj_every_term=1 runs: how many?   A: 9
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_every_term==1  & samp_r3==1 

* Q: run_maj_every_term=1 runs: how many with y>0? A: 7 (78%)
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_every_term==1  & samp_r3==1 & ylt>0

* Q: Maj=0 runs: how many?   A: 36
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_every_term==0  & samp_r3==1 

* Q: Maj=0 runs: how many with y>0? A: 20 (55.6% of nmaj runs)
list country year ylt if lt==1 & run_complete_r3==1 & run_maj_every_term==0  & samp_r3==1 & ylt>0

* Q: first-term residuals - how many <0  A: 25  (55.5%)
list country year xft if lt==1 & run_complete_r3==1 & samp_r3==1 & xft<0

* Q: how many runs in quadrant II, with x<0 & y>0?  A: 13/45  (28.9%)
list country year xft ylt if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt>0 & xft<0 

* Q: how many runs in quadrant IV, with x>0 & y<0?  A: 6/45 = 13.3% 
list country year run_terms xft ylt run_maj_every_term if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt<0 & xft>0 

* Q: how many runs in quadrant I, with x>0 & y>0?  A: 14 (31%)
list country year run_terms xft ylt run_maj_every_term if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt>0 & xft>0 

* Q: how many runs in quadrant III, with x<0 & y<0?  A: 12  (26.67%)  
list country year run_terms xft ylt run_maj_every_term if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt<0 & xft<0 

* Q: how many IBCs? (I already did that in table 6)


*************
* which runs x>0 & y<0?
list country year_ft_end year run_terms run_maj_every_term xft ylt if lt==1 & run_complete_r3==1 & samp_r3==1 & x>0 & y<0 

* which runs are maj=1 
list country year xft ylt if lt==1 & run_complete_r3==1 & run_maj_every_term==1  & samp_r3==1





************ TWO-TERM RUNS
* Q: how many 2t runs in chart?  A: 22, 4 are maj=1
list country year run_maj_every_term ylt xft if lt==1 & run_complete_r3==1 & samp_r3==1 & run_terms==2

* Q: how many 2t runs in quadrant II, with x<0 & y>0?  A: 7 (31.8% of 2-term runs), 1 is maj=1 
list country year run_maj_every_term xft ylt if lt==1 & run_complete_r3==1 & samp_r3==1 & ylt>0 & xft<0 & run_terms==2

* Q: how many 2t runs in quadrant IV?  A: 1 (4.8%), 0 are maj=1 

* Q: 2t runs, if ft<0, what about lt resid?  7/13 are ylt>0
list country year run_maj_every_term xft ylt if lt==1 & run_complete_r3==1 & samp_r3==1 & xft<0 & run_terms==2

* aberrant 2t runs:  NZ90 & Fin95 




